﻿using System.Collections.ObjectModel;
using Unknown.Framework.Configuration;
using Unknown.Source.Entity.Coder;

namespace Unknown.Source.Business.Coder
{
    /// <summary>
    /// 代码配置处理类
    /// </summary>
    public class CodeConfigurationHandler : AbstractConfigurationHandler<Collection<CodeConfiguration>>
    {
        /// <summary>
        /// 匹配字符串
        /// </summary>
        protected override string SearchPattern
        {
            get
            {
                return ".Code.xml";
            }
        }

        /// <summary>
        /// 示例值
        /// </summary>
        protected override Collection<CodeConfiguration> ExampleValue
        {
            get
            {
                Collection<CodeConfiguration> results = new Collection<CodeConfiguration>();
                CodeConfiguration codeConfiguration = new CodeConfiguration();

                codeConfiguration.Separator = "_";
                codeConfiguration.PrimaryKey = "Pkid";

                codeConfiguration.NamespacePrefix = "Unknown.Source";
                codeConfiguration.NamespaceMaps.Add(new NamespaceMap("Z", "Example"));

                codeConfiguration.Constants.Add("Id");
                codeConfiguration.Constants.Add("InsertOperator");
                codeConfiguration.Constants.Add("InsertTime");

                codeConfiguration.Templates.Add(new Template("Entity", "", "", "cs", "Template\\Entity.cs.txt"));
                codeConfiguration.Templates.Add(new Template("Sql", "I", "Sql", "cs", "Template\\ISql.cs.txt"));
                codeConfiguration.Templates.Add(new Template("Sql", "I", "SqlCustom", "cs", "Template\\ISqlCustom.cs.txt"));
                codeConfiguration.Templates.Add(new Template("OracleSql", "", "OperationHandler", "cs", "Template\\OperationHandler.cs.txt"));
                codeConfiguration.Templates.Add(new Template("OracleSql", "", "OracleSql", "cs", "Template\\OracleSql.cs.txt"));
                codeConfiguration.Templates.Add(new Template("OracleSql", "", "OracleSqlCustom", "cs", "Template\\OracleSqlCustom.cs.txt"));
                codeConfiguration.Templates.Add(new Template("Access", "", "Access", "cs", "Template\\Access.cs.txt"));
                codeConfiguration.Templates.Add(new Template("Access", "", "AccessCustom", "cs", "Template\\AccessCustom.cs.txt"));
                codeConfiguration.Templates.Add(new Template("Business", "", "Business", "cs", "Template\\Business.cs.txt"));
                codeConfiguration.Templates.Add(new Template("Business", "", "BusinessCustom", "cs", "Template\\BusinessCustom.cs.txt"));
                codeConfiguration.CodeAnalysis = false;
                codeConfiguration.DataTypeHandler = typeof(IDataTypeHandler).FullName;

                results.Add(codeConfiguration);

                return results;
            }
        }

        /// <summary>
        /// 解析
        /// </summary>
        /// <param name="value">值</param>
        protected override void Resolve(Collection<CodeConfiguration> value)
        {
            if (value != null)
            {
                foreach (CodeConfiguration codeConfiguration in value)
                {
                    if ((codeConfiguration != null) && (!string.IsNullOrEmpty(codeConfiguration.NamespacePrefix)))
                    {
                        string key = codeConfiguration.NamespacePrefix;
                        bool has = Preference.CodeConfigurations.ContainsKey(key);

                        if (!has)
                        {
                            Preference.CodeConfigurations.Add(key, codeConfiguration);
                        }
                    }
                }
            }
        }
    }
}
